﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Vcm
{
    public static class SplittedStringHelper
    {
        public static string[] RegroupItems(ICollection<string> sourceItems, char delimiter)
        {
            const string symbolicQuote = "¤µ¤";
            var result = new string[sourceItems.Count];
            var i = 0;
            var currentString = String.Empty;
            var quoteFound = false;
            foreach (var item in sourceItems)
            {
                var quotesAreOdd = item.Count(c => c == '"') % 2 == 1;
                if (quoteFound)
                {
                    currentString += delimiter + item;
                    if (quotesAreOdd)
                    {
                        result[i++] = item == "\"\""
                            ? string.Empty
                            : currentString.Replace("\"\"", symbolicQuote).Replace("\"", "").Replace(symbolicQuote, "\"");
                        quoteFound = false;
                    }
                }
                else
                {
                    if (quotesAreOdd)
                    {
                        currentString = item;
                        quoteFound = true;
                    }
                    else
                    {
                        result[i++] = item == "\"\""
                            ? string.Empty
                            : item.Replace("\"\"", symbolicQuote).Replace("\"", "").Replace(symbolicQuote, "\"");
                    }
                }
            }
            return result.Take(i).ToArray();
        }

        public static string[] RegroupItems(ICollection<string> sourceItems, char delimiter, char openingDelimiter, char closingDelimiter, bool removeOpeningClosingDelimiters)
        {
            var result = new List<string>(sourceItems.Count);
            var currentString = String.Empty;
            var openingDelimiterFound = false;

            var enumerator = sourceItems.GetEnumerator();
            enumerator.Reset();
            enumerator.MoveNext();
            var item = enumerator.Current;
            while (true)
            {
                var closingDelimiterFound = item.Contains(closingDelimiter);
                if (openingDelimiterFound)
                {
                    currentString += delimiter + item;
                    if (closingDelimiterFound)
                    {
                        var index = currentString.IndexOf(closingDelimiter);
                        var rightPart = currentString.Substring(index + 1);
                        currentString = currentString.Substring(0, index + 1);
                        if (removeOpeningClosingDelimiters)
                            currentString = currentString
                                .Replace(openingDelimiter.ToString(CultureInfo.InvariantCulture), "")
                                .Replace(closingDelimiter.ToString(CultureInfo.InvariantCulture), "");
                        result.Add(currentString);
                        openingDelimiterFound = false;
                        if (rightPart.Length > 0)
                        {
                            item = rightPart;
                            continue;
                        }
                    }
                }
                else
                {
                    openingDelimiterFound = item.Contains(openingDelimiter);
                    if (openingDelimiterFound)
                    {
                        currentString = item;
                        if (closingDelimiterFound)
                        {
                            var index = currentString.IndexOf(closingDelimiter);
                            var rightPart = currentString.Substring(index + 1);
                            currentString = currentString.Substring(0, index + 1);
                            if (removeOpeningClosingDelimiters)
                                currentString = currentString
                                    .Replace(openingDelimiter.ToString(CultureInfo.InvariantCulture), "")
                                    .Replace(closingDelimiter.ToString(CultureInfo.InvariantCulture), "");
                            result.Add(currentString);
                            openingDelimiterFound = false;
                            if (rightPart.Length > 0)
                            {
                                item = rightPart;
                                continue;
                            }
                        }
                    }
                    else
                    {
                        result.Add(item);
                    }
                }
                if (enumerator.MoveNext())
                    item = enumerator.Current;
                else
                    break;
            }
            return result.ToArray();
        }
    }
}